<?php 
//similar_text
require_once 'ckLogin.php';
require_once 'init.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();
//排除相同的评论
//获取所有
$db->sql = "SELECT id,comment FROM `comment` ORDER BY id";
$comment_s = $db->doResultList2();
//var_dump($comment_s);
//$similar_text_array = array();
$i=0;
foreach ($comment_s as $key1 => $item1){
     
    foreach ($comment_s as $key2=>$item2){
        if($key2>$key1){
            similar_text($item2['comment'],$item1['comment'],$percent);
            if($percent>80) {
                $db->sql = "DELETE FROM `comment` WHERE id = {$item2['id']}";
                $db->doExecute();
                $i++;
            }
        }
    }
}
//echo '___'.$i.'___';
//删除
if(!empty($_GET['act'])){
	$act = $_GET['act'];
	if($act == 'del'){
		$db->sql = "DELETE FROM `comment` WHERE id = ".$_GET['id'];
		$db->doExecute();
	}
}
//提交处理
if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
	$cate = $_POST['cate'];
	$cateStr = implode($cate, ',');
	//var_dump($cate);
	$comment = $_POST['comment'];
	$commentID = $_POST['commentID'];
	$db->sql="UPDATE `comment` SET `comment` = '".$comment."' , cates = '".$cateStr."' WHERE id = ".$commentID;
	$db->doExecute();
}
//分类
$db->sql="SELECT * FROM category ORDER BY sort ASC";
$cates = $db->doResultList2();
//条件
$where2 = " WHERE 1=1";
$keyword = $_GET['keyword']?$_GET['keyword']:"";
if ($keyword) {
	$where2 .= " AND `comment` LIKE '%{$keyword}%' ";
}
//总条数
$db->sql="SELECT COUNT(id) FROM comment {$where2}";
$count = $db->doResultOne();
//分页
if(empty($_GET['page'])){$page = 1;}else{$page=$_GET['page'];}
if(empty($_GET['pagesize'])){$pagesize = 50;}
else{$pagesize = $_GET['pagesize'];}
$pageInfo = page($count, $pagesize, $page);
$maxPage = $pageInfo['maxPage'];
$start = $pageInfo['start'];
$pages = $pageInfo['pages'];
//var_dump($pageInfo);
$db->sql="SELECT * FROM comment {$where2} ORDER BY id DESC LIMIT ".$start.",".$pagesize;
$comments = $db->doResultList2();
foreach ($comments as $key => $item){
	if(!empty($item['cates'])){
		$where = "id IN (".$item['cates'].")";
	}else{
		$where = "1=1";
	}
	$db->sql = "SELECT id,cate FROM category WHERE ".$where." ORDER BY sort ASC";
	$catesnames = $db->doResultList2();
	$catesnamelist = array();
	$catesidlist = array();
	foreach ($catesnames as $key2=>$item2){
		$catesnamelist[] = $item2['cate'];
		$catesidlist[] = $item2['id'];
	}
	$comments[$key]['catenames']=implode($catesnamelist, ',');
	$comments[$key]['cateids']=implode($catesidlist, ',');
}

//分页
function page($total,$size,$page){
	//最大页数
	if($total%$size==0){
		$maxPage = $total/$size;
	}else{
		$maxPage = (int)($total/$size)+1;
	}
	//开始条数
	$start = ($page-1)*$size;
	//分页
	if($maxPage>=5){
		if($page<=2){
			$pages=array(1,2,3,4,5);
		}elseif($page>=($maxPage-1)){
			$pages=array($maxPage-4,$maxPage-3,$maxPage-2,$maxPage-1,$maxPage);
		}else{
			$pages=array($page-2,$page-1,$page,$page+1,$page+2);
		}
	}else{
		for($i=1;$i<=$maxPage;$i++){
			$pages[]=$i;
		}
	}
	if($page<=1){$previous=1;}else{$previous=$page-1;}
	if($page>=$maxPage) {$next = $maxPage;}else{ $next = $page+1;}
	return array('maxPage'=>$maxPage,'start'=>$start,'pages'=>$pages,'previous'=>$previous,'next'=>$next);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo APPLICATION;?></title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css" media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />

<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function($) {

	$(".comment-edit").click(function(){
		var id = $(this).parents("tr").children("td").eq(0).text();
		var comment = $(this).parents("tr").children("td").eq(1).text();
		var cateids = $(this).parents("tr").find(".cateids").val();
		//alert(cateids);
		var cateidsArr = cateids.split(",");
		$.each(cateidsArr,function(key,id){
			$(".cateupdate[value="+id+"]").attr("checked", true);
		});
		$("#comment").text(comment);
		$("#commentID").val(id);
		$("#message_click").click();
	});
	$(".deleteUser").click(function(){
		if(!confirm("是否将此评论删除?")){
			return false;
		}
	});
	
});
</script>
<!-- jQuery Configuration -->
<script type="text/javascript"
	src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>

</head>

<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		<a href="#messages" rel="modal" id="message_click"></a>
		<div id="messages" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p style="">
				<strong>简短描述输入及修改</strong>
			</p>
			<form action="<?php echo "?page=".$page;?>" method="post">
				<fieldset>
					分类：
					<?php foreach ($cates as $keyX=>$itemX):?>
					<input class="cateupdate" name="cate[]" type="checkbox" value="<?php echo $itemX['id']?>" /><?php echo $itemX['cate'];?>
					<?php endforeach;?>
					<hr />
					描述：
					<textarea class="textarea" id="comment" name="comment" cols="79" rows="5"></textarea>
					<input id="commentID" name="commentID" type="hidden" value="" />
					<input class="button" type="submit" value="修改" /> 
					<a href="javascript:void(0);" onclick="$(this).parents('#facebox').hide();">关闭</a>
				</fieldset>
			</form>
		</div>
		<!-- End #messages -->
		<div id="main-content">

			<div class="content-box">
				<!-- Start Content Box -->
				<div class="content-box-content">
					<div style="background-color: #E4E4E4; padding:0 0 5px 5px;">
						<div style="padding: 5px 0 0;">
							<form method="get" action="">
								内容：
								<input type="text" class="text-input" id="keyword" name="keyword" value="<?php echo $_GET['keyword'];?>" />
								&nbsp;&nbsp;&nbsp;
								<button class="button" id="zhixing" type="submit">搜索</button>
							</form>
						</div>
					</div>
				<!-- Start Notifications -->
				<?php if($i>0):?>
				<div class="notification success png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
						删除<?php echo $i;?>条相似度》80%的数据。
					</div>
				</div>
				<?php endif;?>
				<div class="notification error png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
					修改失败
					</div>
				</div>
				
				<!-- End Notifications -->
				
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->

						<table>
							<thead>
								<tr>
									<td colspan="5">
									<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="?page=1" title="First Page">&laquo; First</a>
											<a href="?page=<?php echo  $pageInfo['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="?page=<?php echo $item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="?page=<?php echo $pageInfo['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="?page=<?php echo $maxPage;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
									<?php endif;?>
									</td>
								</tr>
							</thead>
							<thead>
								<tr>
									<th>编号</th>
									<th>评论</th>
									<th>分类</th>
									<th>作用日期</th>
									<th>操作</th>
								</tr>

							</thead>

							<tbody>
							<?php if(count($comments)>0):?>
							<?php foreach ($comments as $key=>$item):?>
								<tr>
									<td><?php echo $item['id'];?></td>
									<td><?php echo $item['comment'];?></td>
									<td><?php echo $item['catenames'];?><input class="cateids" type="hidden" value="<?php echo $item['cateids'];?>"/></td>
									<td><?php echo $item['turndate'];?></td>
									<td>
										<a href="javascript:void(0);" class="comment-edit">修改</a>
										<a class="deleteUser" href="?id=<?php echo $item['id'];?>&act=del&page=<?php echo $page;?>">删除</a>
									</td>
								</tr>
							<?php endforeach;?>
							<?php else:?>
								<tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="5" style="text-align: center;">没有评论！！</td></tr>
							<?php endif;?>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="5">
									<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="?page=1" title="First Page">&laquo; First</a>
											<a href="?page=<?php echo  $pageInfo['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="?page=<?php echo $item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="?page=<?php echo $pageInfo['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="?page=<?php echo $maxPage;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
									<?php endif;?>
									</td>
								</tr>
							</tfoot>
						</table>

					</div>
					<!-- End #tab2 -->

				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
